<?php


/**
 * 用户管理
 * Enter description here ...
 * @author Admin
 *
 */
class UserAction extends CommonAction{
	
	
	
	/**
	 * 用户列表
	 * Enter description here ...
	 */
	public function index() {
		$data = Init_GP(array("robot",'mail','name'));  //检索条件：首先进入或者查询都走该方法
		
		$user = D('User');
		
		if (!empty($data)){
			if (!empty($data['robot'])){  //机器人用户1，其他用户0
				$map['robot']=array('eq',$data['robot']);
			}else{
				$map['robot']=array('eq',0);
			}
			if(!empty($data['mail'])){
				$map['mail']=array('like',"%{$data['mail']}%");
			}
			if(!empty($data['name'])){
				$map['name']=array('like',"%{$data['name']}%");
			}
		}
		
		
		import("@.ORG.Page"); //导入分页类
		$count = $user->where($map)->count();    //计算总数
		
		$p = new Page ($count,10);
		$userdata=$user->where($map)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		
		//分页条件写入
		foreach($data as $key=>$val) {
			$p->parameter   .=   "$key=".$val."&";
		}
		
		$page = $p->show ();
		
		$this->assign("page",$page);  //分页信息
		$this->assign("userdata",$userdata);   //结果集
		$this->assign("down",$p->parameter); //参数
		
		$this->display();
	}
	
	
	
	
	
	
	/**
	 * 选择头像
	 * Enter description here ...
	 */
	public function selectHead(){
		$head = M('Head');
		$headdata=$head->field('filename')->order('RAND()')->limit(36)->findAll();  //随即获取36条记录
		
		$this->assign("headdata",$headdata);
		$this->display();
	}
	
	
	
	
	/**
	 * 保存新建用户
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$user = D('User');
		$config = C('SYSCONFIG'); //配置文件
		
		$data = Init_GP(array('mail','name','realname','password','ip','head','sex','birthday','tel','phone','beat','pointb','robot','state','lockstate')); 
		
		if (empty($data['password'])){
			$this->error("密码必须");
		}else {  //密码MD5加密
			$data['password'] = md5($data['password']);
		}
		$data = $user->create($data);
		
		if(empty($data) || $data === false) {
			$this->error($user->getError());
		}
		
		$id = $user->add($data);   //保存用户
		
		if($id) { //保存成功
			
			 //根据配置文件中获取实体币、虚拟币
			//写入日志---交易日志
			$transaction = D('Transaction');  //交易记录
			
			
			if(empty($data['robot'])){   //机器人状态，注册用户未空，机器人传入1
			//	Log::write("普通注册用户" ,Log::DEBUG);
				$robotFlag = 0;
			}else{
		   //	Log::write("机器人" ,Log::DEBUG);
				$robotFlag = $data['robot']  ;
			}
			
			
			if (GetNum($data['beat'])){   //实体竞标币
				$transactiondata = array(
					'uid'=>$id,
					'name' => $data['name'],
					'payment'=>'管理员',
					'money'=>GetNum($data['beat']),
					'addtime'=>time(),
					'remark'=>"管理员编辑用户{$data['beat']}{$config['beat_name']}"  ,
					'type'=>"beat",
					'source'=>'0',
					'robot'=>$robotFlag
				);
				$transaction->add($transactiondata);
			}
			
			
			if (GetNum($data['pointb'])){ //虚拟竞标币
				$transactiondata = array(
					'uid'=>$id,
					'name' => $data['name'],
					'payment'=>'管理员',
					'money'=>GetNum($data['pointb']),
					'addtime'=>time(),
					'remark'=>"管理员编辑用户{$data['pointb']}{$config['pointb_name']}",
					'type'=>"pointb",
					'source'=>'0',
					'robot'=>$robotFlag
				);
				$transaction->add($transactiondata);
			}
			
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
			
		}else {
			$this->error($user->getError());
		}
		
	}
	
	
	
	
	/**
	 * 修改用户信息保存
	 * @see CommonAction::update()
	 */
	public function update(){
		$user = D('User');
		$id     = GetNum($_POST['id']);	   //主键ID
		$config = C('SYSCONFIG');
		
		$userdata = $user->find($id);
		
		$data = Init_GP(array('password','ip','head','sex','birthday','tel','phone','beat','pointb','robot','state','lockstate'));
		$map['id'] = array('eq',$id);
		if (empty($data['password'])){
			unset($data['password']);
		}else {
			$data['password'] = md5($data['password']);
		}
		
		$data = $user->create($data,2);
		if(empty($data) || $data === false) {
			$this->error($user->getError());
		}
		
		$id = $user->where($map)->save($data);
		
		if($id) { //保存成功

			//写入交易记录----新增的实体币和虚拟币
			$beat = $data['beat'] - $userdata['beat'];
			$pointb = $data['pointb'] - $userdata['pointb'];
			
			$transaction = D('Transaction');   
			
			
			if(empty($data['robot'])){   //机器人状态，注册用户未空，机器人传入1
			//	Log::write("普通注册用户" ,Log::DEBUG);
				$robotFlag = 0;
			}else{
		   //	Log::write("机器人" ,Log::DEBUG);
				$robotFlag = $data['robot']  ;
			}
			
			
			if (GetNum($data['beat']) && GetNum($beat)){  //实体竞标币
				$transactiondata = array(
					'uid'=>$userdata['id'],
					'name' => $userdata['name'],
					'payment'=>'管理员',
					'money'=>GetNum($beat),
					'addtime'=>time(),
					'remark'=>"管理员编辑用户{$beat}{$config['beat_name']}",
					'type'=>"beat",
					'source'=>'0',
					'robot'=>$robotFlag
				
				);
				$transaction->add($transactiondata);
			}
			
			
			if (GetNum($data['pointb']) && GetNum($pointb)){   //虚拟竞标币
				$transactiondata = array(
					'uid'=>$userdata['id'],
					'name' => $userdata['name'],
					'payment'=>'管理员',
					'money'=>GetNum($pointb),
					'addtime'=>time(),
					'remark'=>"管理员编辑用户{$pointb}{$config['pointb_name']}",
					'type'=>"pointb",
					'source'=>'0',
					'robot'=>$robotFlag
				);
				$transaction->add($transactiondata);
			}
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("编辑成功");
		}else { //失败提示
			$error = $user->getError();
			if (empty($error)) {
				$this->error("编辑失败");
			}else {
				$this->error($error);
			}
		}
	}
	
	
	
	
	
	
	
	/**
	 * 下载数据文件
	 * Enter description here ...
	 */
	public function down(){
		$data = Init_GP(array("robot",'mail','name'));
		
		$config = C('SYSCONFIG');
		$user = D('User');
		
		if (!empty($data)){
			if (!empty($data['robot'])){
				$map['robot']=array('eq',$data['robot']);
			}else{
				$map['robot']=array('eq',0);
			}
			if(!empty($data['mail'])){
				$map['mail']=array('like',"%{$data['mail']}%");
			}
			if(!empty($data['name'])){
				$map['name']=array('like',"%{$data['name']}%");
			}
		}
		$userdata=$user->where($map)->order('id desc')->findAll();   //获取所有记录
		
		$keynames = array(
			'id'=>'ID',
			'name'=>'用户名',
			'mail'=>'邮箱',
			'realname'=>'真实姓名',
			'sex'=>'性别（0男1女）',
			'tel'=>'电话',
			'beat'=>$config['beat_name'],
			'pointb'=>$config['pointb_name'],
			'addtime'=>'注册时间',
		);
		
		down_xls($userdata, $keynames,'会员信息_'.date("Y-m-d"));
	}
	
	
	

	
	
	
	
	
	
	/**
	 * 赠送用户虚拟币
	 * Enter description here ...
	 */
	public function sendVirtualCoin(){
		$data = Init_GP(array("userId"));
		$userId = $data['userId'];
		
		$user = D('User');
		
		$obj = $user->find($userId);
		$this->assign("data",$obj);
		
		$this->display();
	}
	
	
	
	/**
	 * 赠送用户虚拟币保存
	 * Enter description here ...
	 */
	public function sendVirtualCoinSave(){
	//	Log::write("....赠送用户虚拟币保存start...",Log::DEBUG) ;
		
		$data = Init_GP(array("uid","robot","name","money"));    //接收界面信息
		
		$id = $data['uid'];  //用户ID
		
		//Log::write("....赠送用户虚拟币...用户ID:".$id  ."  赠送竞标币数:" .$data['money']    ,Log::DEBUG) ;
		
		//....修改用户虚拟竞标币数，记录虚拟币历史
		$user = D("User");
		$userdata = $user->find($id);  
		
		$pointb = $data['money'] + $userdata['pointb'];     //虚拟竞标币 = 已有虚拟币 + 赠送虚拟币
		
		//Log::write("....赠送用户虚拟币   虚拟币个数...:".$pointb,Log::DEBUG) ;
		
		$dt['pointb'] = $pointb;
		
		$tt = $user->where('id='.$id)->data($dt)->save();    //保存用户虚拟竞标币数
		
		if($tt){   //如果保存成功，需记录赠送历史
			
				$config = C('SYSCONFIG');
				$transaction = D('Transaction');   //历史记录表
			
				$transactiondata = array(
					'uid'=>$userdata['id'],    //用户ID
					'name' => $userdata['name'], //用户姓名
					'payment'=>'管理员',			//支付人
					'money'=>$data['money'],
					'addtime'=>time(),
					'remark'=>"管理员赠送用户{$data['money']}{$config['pointb_name']}",
					'type'=>"pointb",
					'source'=>'1',
					'robot'=>$data['robot']
				);
				$transaction->add($transactiondata);
			
		}
		
		
	//	Log::write("....赠送用户虚拟币保存end...",Log::DEBUG) ;
		
		
		$this->display();  
		
	}
	
	
	
	
	
	
}
?>